// pages/exportList/exportList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // id:'',
    actId: "",
    type:"",
    fileName:'',
    fileSize:'',
    email:'',
    isOver: false,//自锁
  },
  //获取导出数据信息
  getExcelInfo(){
    var that = this;
    app.req({
      url:"blessmini/birthAct/exportBlessEnrol",
      method:"post",
      data:{
        blessActId:that.data.actId,
        blessActType:that.data.type
      },
      success(res){
        console.log("导出数据",res)
        if(res.code == 200){
          that.setData({
            fileName: res.data.fileName,
            fileSize: res.data.fileSize,
          })
        }
      }
    })
  },
  emailInput(e){
    this.setData({
      email: e.detail.value
    })
  },
  send(){
    if (this.data.isOver){
      return
    }
    var that = this
    var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); 
    if (that.data.email.length == 0) {
      wx.showToast({
        title: "请输入邮箱地址",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!emailPattern.test(that.data.email)) {
      wx.showToast({
        title: "邮箱格式错误",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    that.setData({
      isOver: true,
    })
    app.req({
      url: "blessmini/birthAct/exportBlessEnrolToEmail",
      method: "post",
      data: {
        actId: that.data.actId,
        actType: that.data.type,
        email: that.data.email,
      },
      success: function (res) {
        if (200 == res.code) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          wx.setStorageSync('mail', that.data.email);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail:function(err){
        that.setData({
          isOver: false,
        })
      }
    })
    
  },
  getUserInfo() {
    var that = this
    wx.getSetting({
      success(res) {
        app.getUserInfo(function (userInfo) {
          if (userInfo.mail){
            that.setData({
              email: userInfo.mail,
            })
            wx.setStorageSync('mail', that.data.email);
          }
        }, true)
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      actId: options.actId,
      type: options.type,
    })
    this.getExcelInfo();
    if (wx.getStorageSync('mail') != false) {
      this.setData({
        email: wx.getStorageSync('mail')
      })
    }else{
      this.getUserInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('mail') != false) {
      this.setData({
        email: wx.getStorageSync('mail')
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})